package com.tg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tg.mapper.FriendMapper;
import com.tg.pojo.Friend;
import com.tg.pojo.User;
import com.tg.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;

    @Override
    public List<User> friendInfoList(String username) {
        return friendMapper.getFriendInfoList(username);
    }

    @Override
    public boolean deleteFriend(String username, String friendUsername) {
        LambdaQueryWrapper<Friend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Friend::getUsername, username)
                .eq(Friend::getFriendUsername, friendUsername);
        return friendMapper.delete(lambdaQueryWrapper) > 0;
    }

    @Override
    public boolean sendVerification(Friend friend) {
        int insertCode = friendMapper.insert(friend);
        return insertCode >= 1 ? true : false;
    }

    @Override
    public List<Friend> getVerificationList(String username) {
        LambdaQueryWrapper<Friend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(QueryWrapper -> QueryWrapper.eq(Friend::getUsername,username)
                .or().eq(Friend::getFriendUsername,username))
                .eq(Friend::getStatus,0);
        List<Friend> verificationList = friendMapper.selectList(lambdaQueryWrapper);
        return verificationList;
    }
}
